package com.dscs.service.impl;

import com.dscs.domain.Permission;
import com.dscs.domain.Role;
import com.dscs.domain.User;
import com.dscs.mapper.PermissionMapper;
import com.dscs.mapper.UserMapper;
import com.dscs.service.IUserService;
import com.dscs.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 叶雪娟
 * @date 2019/7/18
 **/
@SuppressWarnings("ALL")
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    @Override
    public List<User> queryUserByIds(String strids) {
        List<Long> ids = new ArrayList();
        String[] str = strids.split(",");
        for (int i = 0; i < str.length; i++) {
            long temp = Integer.valueOf(str[i]);
            ids.add(temp);
        }
        List<User> list = userMapper.queryUserByIds(ids);
        return list;
    }

    @Override
    public User queryUser(long id) {
        return userMapper.queryUser(id);
    }

    @Override
    public Msg insertUser(User user) {
        Msg msg = new Msg();
        long i = userMapper.insertUser(user);
        if (i > 0) {
            msg.setMsg("插入成功。");
            msg.setStatus(true);
        } else {
            msg.setMsg("插入失败。");
            msg.setStatus(false);
        }
        return msg;
    }

    @Override
    public Msg updateUser(User user) {
        Msg msg = new Msg();
        long i = userMapper.updateUser(user);
        if (i > 0) {
            msg.setMsg("更新成功");
            msg.setStatus(true);
        } else {
            msg.setMsg("更新失败");
            msg.setStatus(false);
        }
        return msg;
    }

    @Override
    public Msg deleteUser(long id) {
        Msg msg = new Msg();
        long i = userMapper.deleteUser(id);
        if (i > 0) {
            msg.setMsg("删除成功。");
            msg.setStatus(true);
        } else {
            msg.setMsg("删除失败。");
            msg.setStatus(false);
        }
        return msg;
    }

    @Override
    public List<Permission> queryUserPermission(long id) {
        return permissionMapper.queryUserPermission(id);
    }

    @Override
    public List<Role> queryUserRole(Long id) {
        return userMapper.queryUserRole(id);
    }

    @Override
    public List<User> queryUsersByCondition(User user) {
        return userMapper.queryUsersByCondition(user);
    }


    @Override
    public long updateUserName(String name) {
        return userMapper.updateUserName(name);
    }

    @Override
    public long updateUserAge(long age) {
        return userMapper.updateUserAge(age);
    }

    @Transactional
    @Override
    public Msg updateUserInfo() {
        Msg msg  = new Msg();
        try {
            updateUserName("zhang");
            updateUserAge(15);
            msg.setMsg("成功");
            msg.setStatus(true);
            System.out.println("usermapper  updateUserInfo()");
        }catch (Exception e){
            msg.setMsg("失败");
            msg.setStatus(false);
        }
        return msg;
    }
}
